package com.fashion.web.actions.management.gallery;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.gallery.GalleryColumn;
import com.fashion.service.gallery.GalleryColumnService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("json-default")
@Namespace("/management/gallery")
public class GalleryColumnAction extends ActionSupport {
	private List<GalleryColumn> columns;
	private GalleryColumn column;
	@Resource(name = "galleryColumnService")
	private GalleryColumnService service;
	
	@Action(value = "find_columns", results = {@Result(name = SUCCESS, type = "json", params = {"root", "columns"})})
	public String find() throws Exception {
		columns = service.format(service.findAll());	
		return SUCCESS;
	}
	
	@Action(value = "add_or_update_column", results = {@Result(name = SUCCESS, location = "column.jsp")})
	public String addOrUpdate() throws Exception {
		if(column.getId().equals("-1")){
			service.addColumn(column);	
		}else{
			service.updateColumn(column);
		}
		
		return SUCCESS;
	}
	
	@Action(value = "delete_column", results = {@Result(name = SUCCESS, location = "column.jsp")})
	public String delete() throws Exception {
		service.delColumn(column);	
		return SUCCESS;
	}
	
	@Action(value = "column_index", results = {@Result(name = SUCCESS, location = "column.jsp")})
	public String index() throws Exception{
		return SUCCESS;
	}
	
	public GalleryColumn getColumn() {
		return column;
	}

	public void setColumn(GalleryColumn column) {
		this.column = column;
	}

	public List<GalleryColumn> getColumns() {
		return columns;
	}
	
}